home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / utility / 541 / midi / midipl04 / mp_main.c < prev   
Encoding:
C/C++ Source or Header  |  1991-07-20  |  4.9 KB  |  201 lines

  1. /*
  2.  * File: mp_main.c
  3.  * SGoldthorpe    20-Jul-91
  4.  */
  5.  
  6. /*
  7.  * mp_main - main bit of midiplay
  8.  * This software is (C) 1991 Stephen Goldthorpe but it's FREE!  Usual
  9.  * disclaimers and notices about this software not being sold for profit.
  10.  * But you may take all you want from the code though!  If you have any
  11.  * suggestions/bug fixes please get in contact with me.  I don't want to
  12.  * maintain code i've never even seen before (life's hard enough without all
  13.  * of that)!
  14.  *                        -Steve Goldthorpe
  15.  * Phone (DAYTIME UK):    +44 707 382350
  16.  * Internet E-Mail:    SGoldthorpe.wgc-e@rx.xerox.com
  17.  *            goldthor@arisia.xerox.com
  18.  */
  19.  
  20. #include    <stdio.h>
  21. #include    <types.h>
  22. #include    <stat.h>
  23. #include    <fcntl.h>
  24. #include    <malloc.h>
  25. #include    <errno.h>
  26.  
  27. /* #include    "midiplay.h" included in mp_gbls.h */
  28. #include    "mp_gbls.h"
  29.  
  30. /* EXTERNAL DECLS */
  31. extern int    interp();
  32.  
  33. /* GLOBAL VARIABLES */
  34. BYTE    *file_buff;
  35.  
  36. /* FUNCTION DECLS */
  37. static BOOL    play();
  38. static void    parse_flags(), do_help();
  39.  
  40. /* MAIN CODE - SHORT & SWEET */
  41. int    main(argc,argv)
  42. int    argc;
  43. char    *argv[];
  44. {   int    arg_n;
  45.  
  46.     /* title (so you and I know what's what) */
  47.     printf("%s v %d.%d (%s)\n",app_name,RELEASE,VERSION,DATE);
  48.  
  49.     /* check args */
  50.     if(argc<2)
  51.     {    fprintf(stderr,"%s: -h | {-CPScilmnpt} <file1> {...<fileN>}\n",
  52.         app_name);
  53.     exit();
  54.     };
  55.  
  56.     /* get flags from arg list */
  57.     for(arg_n=1;arg_n<argc;arg_n++)
  58.     if(*argv[arg_n]=='-')
  59.         parse_flags(argv[arg_n]);
  60.  
  61.     /* now get some workspace... */
  62. #ifdef DEBUG
  63.     fprintf(stderr,"%s: malloc (%d)\n",app_name,BUFFER_SIZE);
  64. #endif
  65.     if((file_buff=(BYTE*)malloc(BUFFER_SIZE))==NULL)
  66.     {    fprintf(stderr,"%s: couldn't get enough memory for buffer\n",app_name);
  67.     exit();
  68.     };
  69.  
  70.     /* play all files in arg list */
  71.     for(arg_n=1;arg_n<argc;arg_n++)
  72.     if(*argv[arg_n]!='-')
  73.         if(play(argv[arg_n]))
  74.         break;
  75.  
  76.     /* and tidy up */
  77.     free((char*)file_buff);
  78. };
  79.  
  80. /* FUNCTION DEFS */
  81. static void    parse_flags(flags)
  82. char    *flags;
  83. {   int    c;
  84.     for(c=1;c<strlen(flags);c++)
  85.     switch(*(flags+c))
  86.     {   case 'h':
  87.         do_help();
  88.         exit();
  89.         case 't':
  90.         f_text=TRUE;
  91.         break;
  92.         case 'c':
  93.         f_copyright=TRUE;
  94.         break;
  95.         case 'n':
  96.         f_track_name=TRUE;
  97.         break;
  98.         case 'i':
  99.         f_instrument=TRUE;
  100.         break;
  101.         case 'l':
  102.         f_lyric=TRUE;
  103.         break;
  104.         case 'm':
  105.         f_marker=TRUE;
  106.         break;
  107.         case 'p':
  108.         f_prompt=TRUE;
  109.         break;
  110.         case 'P':
  111.         f_Program=TRUE;
  112.         break;
  113.         case 'C':
  114.         f_Channel_pressure=TRUE;
  115.         break;
  116.         case 'S':
  117.         f_Sysex = TRUE;
  118.         break;
  119.         default:
  120.         fprintf(stderr,"bad flag '%c'\n",*(flags+c));
  121.         break;
  122.         };
  123. };
  124.  
  125. static void    do_help()
  126. {    printf("help\n");
  127.     printf("    %s plays midi files format 0 and 1.\n",app_name);
  128.     printf("syntax:\n");
  129.     printf("    %s -h | {-CPScilmnpt} <file1> {...<fileN>}\n",app_name);
  130.     printf("where\n");
  131.     printf("    h = This help message,\n");
  132.     printf("    C = Send channel pressure data,\n");
  133.     printf("    P = Send program data,\n");
  134.     printf("    S = Send system exclusive data,\n");
  135.     printf("    c = Print copyright messages,\n");
  136.     printf("    i = Print instrument assignments,\n");
  137.     printf("    m = Print song markers,\n");
  138.     printf("    n = Print sequence\track names,\n");
  139.     printf("    p = Print cueing prompts,\n");
  140.     printf("    t = Print general text information.\n\n");
  141.     printf("%s can be stopped by CTRL C and tracks skipped with CTRL S.\
  142. \n",app_name);
  143. };
  144.  
  145. static BOOL    play(file)
  146. char    *file;
  147. {   struct stat    stat_buff;
  148.     WORD    len;
  149.     int        fd;
  150. #ifdef DEBUG
  151.     fprintf(stderr,"%s: play %s\n",app_name,file);
  152. #endif
  153.  
  154.     /* open the file (is possible)  and get info */
  155.     if((fd=open(file,O_RDONLY))<0)
  156.     {    fprintf(stderr,"%s: can't open %s\n",app_name,file);
  157.     return(FALSE);
  158.     };
  159.     if(stat(file,&stat_buff)<0)
  160.     {    perror(app_name);
  161.     return(FALSE);
  162.     };
  163.     if(stat_buff.st_size>BUFFER_SIZE)
  164.     {    fprintf(stderr,"%s: %s too long for buffer\n",app_name,file);
  165.     close(fd);
  166.     return(FALSE);
  167.     };
  168.     len=(WORD)stat_buff.st_size;
  169.  
  170.     /* ...and fill the buffer */
  171.     if(read(fd,(BYTE*)file_buff,len)!=len)
  172.     {    fprintf(stderr,"%s: problem reading %s\n",app_name,file);
  173.     close(fd);
  174.     free((char*)file_buff);
  175.     return(FALSE);
  176.     };
  177.     close(fd);
  178.  
  179.     /* send it to interp for the hard work */
  180.     return(interp(file_buff,file,len));
  181. };
  182.  
  183. /*
  184.  * REVISION LOG
  185.  * ============
  186.  * 0.1    SGoldthorpe    20-Mar-91    Created for Atari ST / Sozobon C.  It's
  187.  *                    a bit atari specific in places but i've
  188.  *                    tried to make it UNIX(tm) looking for
  189.  *                    easier porting (if anyone feels brave
  190.  *                    enough to try.
  191.  * 0.2    SGoldthorpe     7-Apr-91    Messed up the code in mp_intp to
  192.  *                    allow type 1 midi files.  Timing is
  193.  *                    still a bit hairy but it plays 80%
  194.  *                    of the files I have OK.
  195.  * 0.3    SGoldthorpe    27-May-91    Reformatted & tidied up, sorted out
  196.  *                    running status and added flags.
  197.  * 0.4    SGoldthorpe    20-Jul-91    Generally restructed, tidied up and
  198.  *                    added sysex capabilities.
  199.  *
  200.  */
  201.